﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChatContrats;
using System.ServiceModel;
using System.Collections;
using System.Runtime.InteropServices;

namespace Client
{
    public partial class DiscussionPrive : Form
    {
        String destinataire;
        String pseudoUtilisateurCourant;
        IChatWCF chat;
        DuplexChannelFactory<ChatContrats.IChatWCF> channelFactory;
       // [DllImport("user32.dll")]
       // public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
       // private event EventHandler GotFocus;

        public DiscussionPrive(String destinataire, String emmeteur, IChatWCF chat, DuplexChannelFactory<ChatContrats.IChatWCF> channelFactory)
        {
            InitializeComponent();
            this.destinataire = destinataire;
            this.pseudoUtilisateurCourant = emmeteur;
            this.chat = chat;
            this.channelFactory = channelFactory;
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
         
           
        }

        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button1_Click(sender, e);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EnvoyerMessagePrive(this.textBox1.Text);
            textBox1.Clear();
        }

        private void EnvoyerMessagePrive(string message)
        {
            try
            {
               
                    if (channelFactory.State != CommunicationState.Faulted)
                    {
                        this.richTextBox1.AppendText(String.Format("{0} - {1} dit : {2}", DateTime.Now.ToLongTimeString(), this.pseudoUtilisateurCourant, message));
                        richTextBox1.AppendText("\n");
                        chat.EnvoyerMessagePrive(this.pseudoUtilisateurCourant, this.destinataire, message);

                    }
                    else
                    {
                        MessageBox.Show("Le canal de communication est défaillant. Vous allez être déconnecté",
              "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ((discussion)this.Owner).DeconnexionChat();
                        
                    }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DiscussionPrive_FormClosed(object sender, FormClosedEventArgs e)
        {
            ((discussion)this.Owner).discutionPrive.Remove(this.destinataire);
        }

      
        private void DiscussionPrive_Activated(object sender, EventArgs e)
        {

           /* FLASHWINFO fw = new FLASHWINFO();
            fw.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(FLASHWINFO)));
            fw.hwnd = this.Handle;
            fw.dwFlags = 0;
            fw.uCount = UInt32.MaxValue;
            FlashWindowEx(ref fw);*/

          //  FlashWindow.Stop(this);


        }

        


    }
}
